﻿using System.Collections.Generic;
using winS.UnityEditor.Undo;

namespace winS.UnityEditor.ProcessEditor
{
    public class DeleteElementCommand : IUndoCommand
    {
        public virtual string info => "删除元素";

        private readonly StepGraphView stepGraphView;
        private readonly IEnumerable<StepNode> deletedNodes;
        private readonly IEnumerable<ConnectionInfo> connectionInfos;

        public DeleteElementCommand(IEnumerable<StepNode> deletedNodes, IEnumerable<ConnectionInfo> connectionInfos, StepGraphView stepGraphView)
        {
            this.deletedNodes = deletedNodes;
            this.stepGraphView = stepGraphView;
            this.connectionInfos = connectionInfos;
        }

        public void Redo()
        {
            foreach (var deleteInfo in deletedNodes) stepGraphView.RemoveNode(deleteInfo);
            foreach (var connectionInfo in connectionInfos)
            {
                Connectionline connectionline = connectionInfo.connectionline;
                connectionline.Disconnect();
                stepGraphView.RemoveConnectionline(connectionline);
            }
        }
        public void Undo()
        {
            foreach (var deleteInfo in deletedNodes) stepGraphView.AddNode(deleteInfo);
            foreach (var connectionInfo in connectionInfos)
            {
                Connectionline connectionLine = connectionInfo.connectionline;
                connectionLine.Connect(connectionInfo.inputPort, connectionInfo.outputPort);
                stepGraphView.AddConnectionline(connectionLine);
            }
        }
    }
}